//---------------------------------------------------------------------
// Author: jachymko
//
// Description: Base class for commands which require a Path and 
//              LiteralPath parameters.
//
// Creation Date: Dec 23, 2006
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;

namespace Pscx.Commands.Internal
{
    public abstract class PscxPathCommandBase : PscxCmdlet
    {
        protected const string ParameterSetPath = "Path";
        protected const string ParameterSetLiteralPath = "LiteralPath";

        private string[] paths;

        [Parameter(
            ParameterSetName = ParameterSetPath,
            Position = 0, 
            Mandatory = true, 
            ValueFromPipeline = true, 
            ValueFromPipelineByPropertyName = true,
            HelpMessage = "Specifies the path to the file to process. Wildcard syntax is allowed."
        )]
        [AcceptsWildcards(true)]
        public string[] Path
        {
            get { return paths; }
            set { paths = value; }
        }

        [Parameter(
            ParameterSetName = ParameterSetLiteralPath,
            Position = 0, 
            Mandatory = true, 
            ValueFromPipeline = false, 
            ValueFromPipelineByPropertyName = true,
            HelpMessage = "Specifies a path to the item. The value of -LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any characters as escape sequences."
        )]
        [Alias("PSPath")]
        [AcceptsWildcards(false)]
        public string[] LiteralPath
        {
            get { return paths; }
            set { paths = value; }
        }

        protected override void ProcessRecord()
        {
            foreach (string path in GetResolvedPaths())
            {
                WriteVerbose(CmdletName + " processing file " + path);
                ProcessPath(path);
            }
        }

        protected virtual void ProcessPath(string path)
        {
        }

        public string[] GetResolvedPaths()
        {
            List<string> resolved = new List<string>();

            if (paths != null && paths.Length > 0)
            {
                if (ParameterSetName == ParameterSetLiteralPath)
                {
                    foreach (string path in paths)
                    {
                        resolved.Add(this.GetUnresolvedProviderPathFromPSPath(path));
                    }
                }
                else
                {
                    foreach (string path in paths)
                    {
                        ProviderInfo provider;
                        resolved.AddRange(this.GetResolvedProviderPathFromPSPath(path, out provider));
                    }
                }
            }

            return resolved.ToArray();
        }
    }
}
